package com.itheima.controller.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Question;
import com.itheima.service.QuestionService;
import com.itheima.service.impl.QuestionServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/questionServlet")
public class QuestionServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        String method = request.getParameter("method");
        Map resultMap = null;
        if ("findByPage".equals(method)){
            resultMap = this.findByPage(request,response);
        }else if ("add".equals(method)){
            resultMap = this.add(request,response);
        }else if ("update".equals(method)){
            resultMap = this.update(request,response);
        }else if ("delete".equals(method)){
            resultMap = this.delete(request,response);
        }

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(resultMap);
        response.getWriter().write(json);
    }

    private Map delete(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            String id = request.getParameter("id");
            questionService.delete(id);
            resultMap.put("flag",true);
        } catch (Exception e) {
            resultMap.put("flag",false);
            resultMap.put("message","删除失败!");
            e.printStackTrace();
        }
        return resultMap;
    }

    private Map update(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            Map<String, String[]> map = request.getParameterMap();
            Question question = new Question();
            BeanUtils.populate(question,map);
            questionService.update(question);
            resultMap.put("flag",true);
        } catch (Exception e) {
            resultMap.put("flag",false);
            resultMap.put("message","修改失败!");
            e.printStackTrace();
        }
        return resultMap;
    }

    private QuestionService questionService = new QuestionServiceImpl();

    private Map add(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            Map<String, String[]> map = request.getParameterMap();
            Question question = new Question();
            BeanUtils.populate(question,map);
            questionService.add(question);
            resultMap.put("flag",true);
        } catch (Exception e) {
            resultMap.put("flag",false);
            resultMap.put("message","添加失败!");
            e.printStackTrace();
        }

        return resultMap;
    }



    private Map findByPage(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            String currentPageStr = request.getParameter("currentPage");
            int currentPage = 1;
            if (currentPageStr != null && !"".equals(currentPageStr)){
                currentPage = Integer.parseInt(currentPageStr);
            }
            PageInfo info = questionService.findByPage(currentPage);
            resultMap.put("flag",true);
            resultMap.put("resultData",info);
        } catch (Exception e) {
            resultMap.put("flag",false);
            resultMap.put("message","查询失败!");
            e.printStackTrace();
        }
        return resultMap;
    }
}
